package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.behavior.thread.AppThreadLocalUtil;
import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {

    @Autowired
    private CacheService cacheService;

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null ||  user.getId() == null || user.getId() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        Integer userId = user.getId();
        Long articleId = dto.getArticleId();
        Short type = dto.getType();

        if (type == 0){
            cacheService.hPut(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId,userId.toString(),JSON.toJSONString(dto));
        }else if(type == 1){
            cacheService.hDelete(BehaviorConstants.UN_LIKE_BEHAVIOR + articleId,userId.toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null ||  user.getId() == null || user.getId() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        Integer userId = user.getId();
        Long articleId = dto.getArticleId();
        Short count = dto.getCount();

        String o = (String) cacheService.hGet(BehaviorConstants.READ_BEHAVIOR + articleId, userId.toString());
        if (o != null){
            //证明已经阅读过，需要数量累加
            ReadBehaviorDto readBehaviorDto = JSON.parseObject(o, ReadBehaviorDto.class);
            Short oldCount = readBehaviorDto.getCount();
            Short newCount = (short)(oldCount + count);
            dto.setCount(newCount);
        }

        cacheService.hPut(BehaviorConstants.READ_BEHAVIOR + articleId,userId.toString(),JSON.toJSONString(dto));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null ||  user.getId() == null || user.getId() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }

        Integer userId = user.getId();
        Long articleId = dto.getArticleId();
        Short operation = dto.getOperation();

        if (operation == 0){
            //点赞
            Object o = cacheService.hGet(BehaviorConstants.LIKE_BEHAVIOR + articleId, userId.toString());
            if (o != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"不可重复点赞");
            }

            cacheService.hPut(BehaviorConstants.LIKE_BEHAVIOR + articleId,userId.toString(), JSON.toJSONString(dto));
        }else if (operation == 1){
            //不点赞
            cacheService.hDelete(BehaviorConstants.LIKE_BEHAVIOR + articleId, userId.toString());
        }
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
